<?php

namespace App\Http\Controllers\Admin;

use App\Device;
use App\Http\Controllers\Admin\Scene\Service\Spot;
use App\Repositories\DeviceRepository;
use App\Service\WechatDeviceService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class DeviceController extends Controller
{
    protected $deviceRepository;
    protected $deviceService;
    private $routePrefix = 'admin.scene.spot';

    public function __construct()
    {
        $this->deviceRepository = new DeviceRepository();
        $this->deviceService = new WechatDeviceService();
    }

    public function index(Request $request)
    {
        return view('admin.device.index');
    }

    //ajax
    public function ajaxList(Request $request)
    {
        $list = $this->deviceRepository->getListPage($request);

        return $this->page_json_render($list);
    }

    public function add()
    {
        return view('admin.device.edit_add');
    }

    public function store(Request $request)
    {

        $this->validate($request, [
            'comment' => 'required',
            'quantity' => 'required',
            'apply_reason' => 'required'
        ]);
        // dd($request->all());
        //微信申请设备
        $device=$this->deviceRepository->storeApplyId($request);

      if($device) {
          return redirect()->route('admin.device')->with(
              $this->success()
          );
      }

    }

    public function show($id)
    {
        $device = Device::with('spot')->find($id);
        return view('admin.device.show', compact('device', 'id'));
    }

    public function spot(Request $request, $id)
    {

        $manager = new Spot();

        if (isset($request->act)) {
            $act = $request->act;
            return $this->{$act}();
        } else {

            return view('admin.device.select_spot', [
                'status' => $manager->getStatusList(),
                'manager' => $manager,
                'routePrefix' => $this->routePrefix,
                'device_id' => $id
            ]);
        }
    }

    public function bind($device_id, $id)
    {
        $device = Device::findOrFail($device_id);
        $page = \App\Http\Controllers\Admin\Scene\Repository\Spot::findOrFail($id);
        $response = $this->deviceService->bindpage($device->device_id, $device->uuid, $device->major, $device->minor, [(int)                    $page->page_id]);

        if ($response['errcode'] == 0) {
            $device->page_id = $page->page_id;
            $device->save();
            return [
                'info' => 'success',
                'msg' => $page->title
            ];
        } else {
            return [
                'info' => 'error',
                'msg' => '微信未配置'
            ];
        }
    }

    public function edit(Request $request,$device_id){
        $comment=$request->comment;
        $device=Device::findOrFail($device_id);
        $updateMessage=$this->deviceService->update($device->device_id,$device->uuid,$device->major,$device->minor,$device->comment);
        if($updateMessage['errcode']==0){
            $device->comment=$comment;
            $device->save();
            return [
                'info'=>'success',
                'msg'=>''
            ];
        }else{
            return [
                'info'=>'error',
                'msg'=>$updateMessage['errmsg']
            ];
        }
    }
    //删除设备 todo:: 暂无删除设备接口
    public function del($id){
        $device=Device::findOrFail($id);

    }

}
